package com.yoo.ons.dialog

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.gyf.immersionbar.ImmersionBar
import com.yoo.module_base.utils.ScreenUtils
import com.yoo.module_base.utils.date.DateUtil
import com.yoo.module_base.utils.setOnClickCallback
import com.yoo.module_base.utils.setSystemBottomNavBarColor
import com.yoo.module_base.utils.setSystemBottomNavBarNoColor
import com.yoo.module_base.view.DateWheelLayout
import com.yoo.module_base.view.EnglishDateFormatter
import com.yoo.ons.R
import razerdp.basepopup.BasePopupWindow
import java.util.Calendar


class UpdateBirthDatePopup(activity: FragmentActivity, var birthdate:String, var callBack:(name:String)->Unit):BasePopupWindow(activity) {

    private var selectDateValue=""
    init {
        setContentView(R.layout.popup_update_birth_date)
        initView(activity)
        showAnimation=  AnimationUtils.loadAnimation(activity, com.yoo.module_res.R.anim.anims_slide_in_bottom)
        dismissAnimation=  AnimationUtils.loadAnimation(activity, com.yoo.module_res.R.anim.anims_slide_out_bottom)
    }

    fun initView(activity:FragmentActivity){
        val popupClose = findViewById<ImageView>(R.id.popup_close)
        val dateSelectLayout = findViewById<DateWheelLayout>(R.id.date_select_layout)
        val popupUpdateBirthDateSave = findViewById<TextView>(R.id.popup_update_birth_date_save)
        popupClose.setOnClickCallback {
            dismiss()
        }


        val split = birthdate.split("-")
        //日期控件参数设置
        dateSelectLayout.setDateMode(DateMode.YEAR_MONTH_DAY)
        val c = Calendar.getInstance()
        //设置最小可选年份
        c.add(Calendar.YEAR, -18)
        //设置当前选择的年月日
        val target = DateEntity.target(split[2].toInt(),DateUtil.replace(split[0]).toInt(),split[1].toInt())
        dateSelectLayout.setRange(
            DateEntity.target(c.weekYear-42, 1, 1),
            DateEntity.target(c),
            target
        )
        dateSelectLayout.setVisibleItemCount(5)
        dateSelectLayout.setItemSpace(ScreenUtils.dip2px(activity,45f))
        dateSelectLayout.setAtmosphericEnabled(false)
        dateSelectLayout.setIndicatorEnabled(true)
        dateSelectLayout.setIndicatorColor(ContextCompat.getColor(activity, com.yoo.module_res.R.color.color_D34EFF))
        dateSelectLayout.setIndicatorSize(ScreenUtils.dp2pxF(activity,1f))
        dateSelectLayout.setTextColor(Color.WHITE)
        dateSelectLayout.setSelectedTextColor(ContextCompat.getColor(activity, com.yoo.module_res.R.color.color_FF3EAB))
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            dateSelectLayout.dayWheelView.typeface = context.resources.getFont(com.yoo.module_res.R.font.lexenddeca_medium)
            dateSelectLayout.monthWheelView.typeface = context.resources.getFont(com.yoo.module_res.R.font.lexenddeca_medium)
            dateSelectLayout.yearWheelView.typeface = context.resources.getFont(com.yoo.module_res.R.font.lexenddeca_medium)
        }
        dateSelectLayout.setOnDateSelectedListener { year, month, day ->
            selectDateValue="${DateUtil.getMonths()[month-1]}-${EnglishDateFormatter().formatDay(day)}-$year"
        }

        popupUpdateBirthDateSave.setOnClickCallback {
            callBack.invoke(selectDateValue)
            dismiss()
        }



        //设置底部导航栏颜色
        setSystemBottomNavBarColor(activity)
        onDismissListener =object : OnDismissListener() {
            override fun onDismiss() {
                setSystemBottomNavBarNoColor(activity)
            }

        }
    }
}